package com.example.lenovo.sbns.Activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.GetCallback;
import com.example.lenovo.sbns.R;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 49670 on 2016/6/12.
 */
public class talk_info extends Activity {

    private ImageView photoView;
    private RadioButton first,second,third;
    private TextView usernameView;
    private TextView timeView;
    private TextView titleView;
    private TextView contentView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.talk_info);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.talk_info_title);

        usernameView = (TextView) findViewById(R.id.talk_inner_username);
        timeView = (TextView) findViewById(R.id.talk_inner_time);
        titleView = (TextView) findViewById(R.id.talk_inner_title);
        contentView = (TextView) findViewById(R.id.talk_inner_content);
        photoView = (ImageView) findViewById(R.id.talk_inner_img);


        /*从Intent中获取数据*/
        Intent intent = getIntent();
        String talkid = intent.getStringExtra("talkid");

        /*连接数据库*/
        AVOSCloud.initialize(this, "Po6OudEc9QxRgv1qcussxBxi-gzGzoHsz", "EFNsW7y5aXtKMkpU50xyhHNk");
        AVQuery<AVObject> query = new AVQuery<>("Talk");
        System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+talkid);
        query.whereEqualTo("objectId", talkid);
        query.getInBackground(talkid, new GetCallback<AVObject>() {
            @Override
            public void done(AVObject avObject, AVException e) {
                usernameView.setText(avObject.getString("talkname"));
                timeView.setText(avObject.getString("talktime"));
                titleView.setText(avObject.getString("talktitle"));
                contentView.setText(avObject.getString("talkcontent"));

                final Map<String, Object> map = new HashMap<String, Object>();
                final Handler handler = new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        map.put("photo", (Drawable) msg.obj);
                        photoView.setImageDrawable((Drawable) map.get("photo"));
                    }

                };
                final String url = avObject.getString("talkpicture");
                // image
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        //post() is quite important,update pictures in UI main thread
                        Drawable drawable = loadImageFromNetwork(url);
                        Message message = handler.obtainMessage(1, drawable);
                        handler.sendMessage(message);
                    }
                }).start();  //线程启动

            }
        });




        first = (RadioButton) findViewById(R.id.FirstButtonTwo);
        second = (RadioButton) findViewById(R.id.SecondButtonTwo);
        third = (RadioButton) findViewById(R.id.ThirdButtonTwo);

    }

    //download image from network using @urladdress
    private Drawable loadImageFromNetwork(String urladdr) {
        // TODO Auto-generated method stub
        Drawable drawable = null;
        try{
            //judge if has picture locate or not according to filename
            drawable = Drawable.createFromStream(new URL(urladdr).openStream(), "image.jpg");
        }catch(IOException e){
            Log.d("test", e.getMessage());
        }
        if(drawable == null){
            Log.d("test","null drawable");
        } else {
            Log.d("test","not null drawable");
        }
        return drawable;
    }
}
